package com.twu.biblioteca;

import com.twu.biblioteca.Commands.*;

import java.util.HashMap;
import java.util.Scanner;

public class BibliotecaApp {
    private Command[] commands = {new ViewBooksCommand(), new ViewMoviesCommand(), new ReserveBookCommand(), new CheckMembershipCommand(), new QuitCommand()};
    private Library lib = null;
    private static final String welcomeMessage = "Welcome!";
    private static final String menuMessage = "0 - List books in library\n" +
            "1 - List movies in library\n" +
            "2 - Reserve book\n" +
            "3 - Check membership\n" +
            "4 - Quit";

    BibliotecaApp() {
        lib = new Library();
        lib.addBook(new Book("Harry Potter"));
        lib.addBook(new Book("Mountains Beyond Mountains"));
        lib.addBook(new Book("How to Kill a Mockingbird"));
        lib.addMovie(new Movie("Movie1", "Director1", 2013, 1));
        lib.addMovie(new Movie("Movie2", "Director2", 2000, 2));
        lib.addMovie(new Movie("Movie3", "Director3", 1967, -1));
        lib.addMovie(new Movie("Movie4", "Director1", 201, 1));
        lib.addMovie(new Movie("Movie5", "Director2", 2, 2));
        lib.addMovie(new Movie("Movie6", "Director3", 52, 10));
        lib.addMovie(new Movie("Movie7", "Director1", 1997, 3));
        lib.addMovie(new Movie("Movie8", "Director2", 1992, 8));
        lib.addMovie(new Movie("Movie9", "Director3", 2000, 1));
        lib.addMovie(new Movie("Movie10", "Director1", 2013, 7));
        lib.addMovie(new Movie("Movie11", "Director2", 2002, 2));
        lib.addMovie(new Movie("Movie12", "Director3", 1923, 4));
        lib.addMovie(new Movie("Movie13", "Director1", 1800, 6));
        lib.addMovie(new Movie("Movie14", "Director2", 1850, 2));
        lib.addMovie(new Movie("Movie15", "Director3", 1992, 7));
    }

    public static void main(String[] args) {
        HashMap<String, User> users = new HashMap<String, User>();
        users.put("111-1111", new User("password", "user1", "user1@email.com", "012-313-5320"));
        users.put("111-1112", new User("password2", "user2", "user2@email.com", "012-313-5321"));
        users.put("111-1113", new User("password3", "user3", "user3@email.com", "963-248-6190"));

        new BibliotecaApp().run(new Scanner(System.in), new LogInSystem(users));
    }

    public void run(Scanner scanner, LogInSystem logInSystem) {
        System.out.println(welcomeMessage);
        attemptUserLogin(scanner, logInSystem);
        int userCommandSelection = 0;
        while (!commands[userCommandSelection].getClass().equals(QuitCommand.class)) {
            System.out.println(menuMessage);
            userCommandSelection = getUserCommand(scanner);
            System.out.println(commands[userCommandSelection].execute(lib, scanner, logInSystem));
        }
    }

    private int getUserCommand(Scanner scanner) {
        int userCommand = scanner.nextInt();
        while (!commandIsValid(userCommand)) {
            System.out.println("Select a valid option!!");
            userCommand = scanner.nextInt();
        }
        return userCommand;
    }

    private boolean commandIsValid(int userCommand) {
        return (userCommand >= 0 && userCommand < commands.length);
    }

    public void attemptUserLogin(Scanner scanner, LogInSystem logInSystem) {
        System.out.println("Please enter username.");
        String username = scanner.nextLine();
        System.out.println("Please enter password.");
        String password = scanner.nextLine();

        if (!logInSystem.validateUsernameAndPassword(username, password))
            System.out.println("Login was invalid. You will be a guest for this session.");
        else
            System.out.println("Login was valid! Welcome!");

    }
}
